package com.example.composableexample.animate.navigation

import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandIn
import androidx.compose.animation.shrinkOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController


@OptIn(ExperimentalAnimationApi::class)
@Preview
@Composable
fun NavigationAnimateSimple() {
    val navController = rememberAnimatedNavController()
    AppNavigation(navController = navController)
}


@OptIn(ExperimentalAnimationApi::class)
@Composable
fun AppNavigation(navController: NavHostController) {
    AnimatedNavHost(
        navController = navController,
        startDestination = "home",
        enterTransition = {
            expandIn(animationSpec = tween(durationMillis = 800))
        },
        exitTransition = {
            shrinkOut(animationSpec = tween(durationMillis = 800))
        }
    ) {
        composable(
            route = "home",
            enterTransition = {
                if (initialState.destination.route == "event") {
                    slideIntoContainer(
                        AnimatedContentScope.SlideDirection.Right,
                        animationSpec = tween(600)
                    )
                } else {
                    null
                }
            },
            exitTransition = {
                if (targetState.destination.route == "event") {
                    slideOutOfContainer(
                        AnimatedContentScope.SlideDirection.Left,
                        animationSpec = tween(600)
                    )
                } else {
                    null
                }
            }
        ) {
            NavigationHome(navController = navController)
        }

        composable(
            route = "event",
            enterTransition = {
                if (initialState.destination.route == "home") {
                    slideIntoContainer(
                        AnimatedContentScope.SlideDirection.Left,
                        animationSpec = tween(600)
                    )
                } else {
                    null
                }
            },
            exitTransition = {
                if (targetState.destination.route == "home") {
                    slideOutOfContainer(
                        AnimatedContentScope.SlideDirection.Right,
                        animationSpec = tween(600)
                    )
                } else {
                    null
                }
            }
        ) {
            NavigationEvent(navController = navController)
        }
    }
}

@Composable
fun NavigationHome(navController: NavHostController) {
    Box(
        modifier = Modifier
            .fillMaxSize().background(brush = Brush.linearGradient(colors = listOf(Color.Blue, Color.Magenta, Color.Cyan)))
    ) {
        Text(
            text = "Home", modifier = Modifier
                .padding(16.dp)
                .align(Alignment.TopCenter)
        )
        Button(
            onClick = { navController.navigate("event") },
            modifier = Modifier.align(Alignment.Center)
        ) {
            Text(text = "跳转到Event")
        }

    }
}

@Composable
fun NavigationEvent(navController: NavHostController) {
    Box(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Text(
            text = "Event", modifier = Modifier
                .padding(16.dp)
                .align(Alignment.TopCenter)
        )
        Button(
            onClick = { navController.navigate("home") },
            modifier = Modifier.align(Alignment.Center)
        ) {
            Text(text = "跳转到Home")
        }

    }
}

